
var data=JSON.parse(sessionStorage.getItem("key"));

console.log("1",data);                     //角色生成对象



var read=JSON.parse(sessionStorage.getItem('playerState'));
console.log('3',read);












//有限状态机

var day=1;
for(let i=0; i<read.length;i++){
    if(read[i].cause ==="已经挂了"){

        day++;
    }
}
console.log("/",day);
//创建天数
var time=["1","2","3","4","5","6","7","8"];
for(let i=0; i<day-1;i++) {
    $('.main-d').eq(0).append($('#bout').clone(true));
}

    for(let e = 0; e<=day; e++){
        $('.h-day').eq(e).text('第'+time[e]+'天');

    }

    for(let e=0; e+1 <=day -1;e++){
        $('.day-s').eq(e).css('display','none')
    }



    $('.m-day').click(function () {
        $(this).next().toggle();
    });


//有限状态机
var start = sessionStorage.getItem('begin');

console.log("状态机",start);


var fsm = new StateMachine({

       init:start,
    transitions:[          //从固态到业态
        { name:'kill',  from:'start', to:'killed'},    //过度要有个过度的条件去触发过度
        { name:'talk',  from:'killed',to:'told'},
        { name:'speak', from:'told',  to:'spoke'},
        { name:'vote',  from:'spoke', to:'vote'},


    ],

    //构造状态机的方法

    methods: {  //从固态到业态会发生什么这里定义
        // //没有按照顺序提示                                  /* 下面第一个函数是报错处理机制，当不按转换顺序，状态机会报错并停止运行程序； */
        onInvalidTransition: function (transition,from,to) {
            switch (from) {
                case 'start':
                    alert("请按游戏顺序进行，杀手先杀人！");
                    break;
                case 'killed':
                    alert("请按游戏顺序进行，亡灵发言！");
                    break;
                case 'told':
                    alert("请按游戏顺序进行，玩家发言！");
                    break;
                case 'spoke':
                    alert("请按游戏顺序进行，玩家投票！");
                    break;

            }
        },


        // onKill: function () {
        //     console.log("AFTER:");
        //     window.location.href = "task4-2.html";
        // },
        //
        // onTalk: function () {
        //     console.log("AFTER2");
        //     alert("确认发言");
        // },



    }

    });





//状态机触发事件
// var kill = document.getElementById("result");
//
//
// kill.onclick = function () {
//     if(fsm.state ==='start'){
//         fsm.kill();
//
//
//     }else {
//         alert('请按游戏流程操作')
//     }
// };


//添加点击事件

$('.kill').eq(day-1).click(function () {

   if(fsm.state === 'start'){
    sessionStorage.setItem('begin', 'killed');
    // console.log('1');
    alert('天黑了，杀手开始杀人');
    window.location = "task4-2.html";
       console.log(fsm.can('kill'),fsm.state);
}
    fsm.kill();//触发时调用
});

$('.talk').eq(day-1).click(function () {
    if(fsm.state ==='killed'){
    console.log(fsm.can('talk'));
    sessionStorage.setItem('begin','told');
    // console.log('2');
    alert('请幽灵发表遗言');
    }
    fsm.talk();
});

$('.speak').eq(day-1).click(function () {
    if(fsm.state === 'told'){
        console.log(fsm.can('speak'));
    sessionStorage.setItem('begin', 'spoke');
    // console.log('3');
    alert('请玩家发言');
}
    fsm.speak();
});


$('.vote').eq(day-1).click(function () {
    if(fsm.state ==='spoke') {
        console.log(fsm.can('vote'));
        sessionStorage.setItem('begin', 'start');
        // console.log('4');
        window.location = "task4-3.html";
        alert('请开始投票');
    }

    fsm.vote();

});

